package com.cxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_evaluate;
import com.common.utils.StringUtils;
import com.cxh.dao.A_evaluateDao;
import com.cxh.dao.impl.A_evaluateDaoImpl;

public class A_evaluateServlet extends HttpServlet {
	A_evaluateDao ed = null;
	StringUtils su = null;
	@Override
	public void init() throws ServletException {
		ed = new A_evaluateDaoImpl();
		su = new StringUtils();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String opr = req.getParameter("opr");
		PrintWriter out = resp.getWriter();
		String errmsg = "";
		int rows=0;
		if("delone".equals(opr))
			delone(req, resp, out, errmsg,rows);
		else if("delsome".equals(opr))
			delsome(req, out, errmsg, rows);
		else if("unread".equals(opr)||"read".equals(opr))
			read_or_notList(req, resp, opr);
		else if("readchange".equals(opr))
			changeread(req, out, errmsg, rows);
		else
			evaList(req, resp,null);
	}
	private void changeread(HttpServletRequest req, PrintWriter out,
			String errmsg, int rows) {
		Map<String,Object> mp = new HashMap<String,Object>();
		try{	
			mp.put("eva_id", su.str2Integer(req.getParameter("eva_id"), null));
			mp.put("eva_msg", req.getParameter("eva_msg"));
			mp.put("eva_score", su.str2Integer(req.getParameter("eva_score"), null));
			mp.put("customer_id", su.str2Integer(req.getParameter("customer_id"), null));
			mp.put("eva_date", su.str2DateBySql(req.getParameter("eva_date"), null, new Date(), true));
			mp.put("issee", 1);
			rows = ed.updateA_evaluate(mp);
		}catch(Exception e){
			errmsg = e.getMessage();
		}
		List<A_evaluate> le = ed.findByCondition(null);
		req.setAttribute("list_e", le);
		if(rows>0)
			out.println("<script type='text/javascript'>location.href='evaluateServlet';</script>");
		else
			out.println("<script type='text/javascript'>alert('修改失败，原因:+"+errmsg+"');location.href='evaluateServlet';</script>");
	}
	private void delone(HttpServletRequest req, HttpServletResponse resp,
			PrintWriter out, String errmsg, int rows) throws ServletException,
			IOException {	
		int eva_id = 0;
		try{
			eva_id = su.str2Integer(req.getParameter("eva_id"), 0);		
			rows = ed.deleteA_evaluate(eva_id);
		}catch(Exception e){
			errmsg = e.getMessage();
		}
		List<A_evaluate> le = ed.findByCondition(null);
		req.setAttribute("list_e", le);
		if(rows>0)
			out.println("<script type='text/javascript'>alert('删除完成');location.href='evaluateServlet';</script>");
		else
			out.println("<script type='text/javascript'>alert('删除失败，原因:+"+errmsg+"');location.href='evaluateServlet';</script>");
	}
	private void read_or_notList(HttpServletRequest req,
			HttpServletResponse resp, String opr) throws ServletException,
			IOException {
		Map<String,Object> mp = new HashMap<String,Object>();
		if("unread".equals(opr))
			mp.put("issee", 0);
		else
			mp.put("issee", 1);
		evaList(req, resp, mp);
	}
	private void delsome(HttpServletRequest req, PrintWriter out,
			String errmsg, int rows) {
		int row = 0;
		String del_list = req.getParameter("del_list");
		String[] eva_ids = del_list.split("|");
		for(String eid:eva_ids){
			int id = 0;
			try{	
				id = su.str2Integer(eid, null);
				row = ed.deleteA_evaluate(id);
			}catch(Exception e){
				errmsg = e.getMessage();
			}
			rows+=row;
		}
		List<A_evaluate> le = ed.findByCondition(null);
		req.setAttribute("list_e", le);
		if(rows>0)
			if(rows-eva_ids.length==0)
				out.println("<script type='text/javascript'>alert('共"+eva_ids.length+"条记录删除完成');</script>");
			else
				out.println("<script type='text/javascript'>alert('共"+rows+"条记录删除完成，"+(eva_ids.length-rows)+"条记录删除失败，原因:+"+errmsg+"');</script>");
		else
			out.println("<script type='text/javascript'>alert('全部删除失败，原因:+"+errmsg+"');</script>");
	}
		
	private void evaList(HttpServletRequest req, HttpServletResponse resp, Map<String,Object> mp)
			throws ServletException, IOException {
		List<A_evaluate> le = ed.findByCondition(mp);
		req.setAttribute("list_e", le);
		req.getRequestDispatcher("EvaluateManage/evaluate_list.jsp").forward(req, resp);
	}
}
